<?php
/**
 * Minhlog 后台页面功能控制器
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Controller
 * @package    Backend
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: ListController.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class Backend_ListController extends Minh_Controller_Action
{
    protected $_articleModel;
    protected $_categoryModel;
    protected $_tagModel;
    protected $_commentModel;
    protected $_pageModel;
    protected $_linkModel;
    protected $_userModel;

    public function init()
    {
        if (!isset($_SESSION['Minhlog']['storage']->user_id)) {
            $this->_redirect('/backend/user/login/');
        }

        if(null === $this->_articleModel){
            if(class_exists('Domain_Model_Article')){
                $this->_articleModel = new Domain_Model_Article();
            } else {
                throw new Exception('文章模型类不存在!');
            }
        }

        if(null === $this->_categoryModel){
            if(class_exists('Domain_Model_Category')){
                $this->_categoryModel = new Domain_Model_Category();
            } else {
                throw new Exception('文章分类模型类不存在!');
            }
        }

        if(null === $this->_tagModel){
            if(class_exists('Domain_Model_Tag')){
                $this->_tagModel = new Domain_Model_Tag();
            } else {
                throw new Exception('标签模型类不存在!');
            }
        }

        if(null === $this->_commentModel){
            if(class_exists('Domain_Model_Comment')){
                $this->_commentModel = new Domain_Model_Comment();
            } else {
                throw new Exception('文章评论模型类不存在!');
            }
        }

        if(null === $this->_pageModel){
            if(class_exists('Domain_Model_Page')){
                $this->_pageModel = new Domain_Model_Page();
            } else {
                throw new Exception('页面模型类不存在!');
            }
        }

        if(null === $this->_linkModel){
            if(class_exists('Domain_Model_Link')){
                $this->_linkModel = new Domain_Model_Link();
            } else {
                throw new Exception('友情链接模型类不存在!');
            }
        }

        if(null === $this->_userModel){
            if(class_exists('Domain_Model_User')){
                $this->_userModel = new Domain_Model_User();
            } else {
                throw new Exception('用户模型类不存在!');
            }
        }

        parent::init();
    }

    public function articleAction ()
    {
        $filter = new Zend_Filter_Int();
        $pageNum = $filter->filter($this->_getParam('page', 1));

        $resultSet = $this->_articleModel->getAllArticle();
        $perPageNum = 10;

        if(null === $resultSet){
            $articleList = array();
        } else {
            $articleList = Zend_Paginator::factory($resultSet);
            $articleList->setCurrentPageNumber($pageNum)->setItemCountPerPage($perPageNum);
            $this->view->articleList = $articleList;
        }

        $this->view->menuSelected = 2;
        echo $this->view->render('articlelist.phtml');
    }

    public function categoryAction ()
    {
        $filter = new Zend_Filter_Int();
        $pageNum = $filter->filter($this->_getParam('page', 1));
        $categoryId = $filter->filter($this->_getParam('id'));

        if($categoryId > 0){
            $categoryInfo = $this->_categoryModel->getCategoryById($categoryId);
            $this->view->categoryInfo = $categoryInfo;
        }else{
            $resultSet = $this->_categoryModel->getAllCategory();
            if(null === $resultSet){
                $categoryList = array();
            } else {
                $perPageNum = 10;
                $categoryList = Zend_Paginator::factory($resultSet);
                $categoryList->setCurrentPageNumber($pageNum)->setItemCountPerPage($perPageNum);
            }
            $this->view->categoryList = $categoryList;
        }

        $parentCategory = $this->_categoryModel->getAllCategory(0);
        if(null === $parentCategory){
            $parentCategory = array();
        }else{
            $this->view->parentCategory = $parentCategory;
        }
        $this->view->categoryId = $categoryId;
        $this->view->menuSelected = 3;
        echo $this->view->render('category.phtml');
    }

    public function tagAction ()
    {
        $filter = new Zend_Filter_Int();
        $pageNum = $filter->filter($this->_getParam('page', 1));
        $tagId = $filter->filter($this->_getParam('id'));

        if($tagId > 0){
            $tagInfo = $this->_tagModel->getTagById($tagId);
            $this->view->tagInfo = $tagInfo;
        }else{
            $resultSet = $this->_tagModel->getAllTag();
            if(null === $resultSet){
                $tagList = array();
            } else {
                $perPageNum = 10;
                $tagList = Zend_Paginator::factory($resultSet);
                $tagList->setCurrentPageNumber($pageNum)->setItemCountPerPage($perPageNum);
            }
            $this->view->tagList = $tagList;
        }

        $this->view->tagId = $tagId;
        $this->view->menuSelected = 4;
        echo $this->view->render('tag.phtml');
    }

    public function commentAction ()
    {
        $filter = new Zend_Filter_Int();
        $pageNum = $filter->filter($this->_getParam('page', 1));
        $commentId = $filter->filter($this->_getParam('id'), 0);

        if($commentId > 0){
            $commentInfo = $this->_commentModel->getCommentById($commentId);
            $this->view->commentInfo = $commentInfo;
        }else{
            $resultSet = $this->_commentModel->getAllComment();
            if(null === $resultSet){
                $commentList = array();
            } else {
                foreach($resultSet as $result){
                    $result->replyTime = date('Y-m-d H:i:s', $result->replyTime);
                }
                $perPageNum = 10;
                $commentList = Zend_Paginator::factory($resultSet);
                $commentList->setCurrentPageNumber($pageNum)->setItemCountPerPage($perPageNum);
            }
            $this->view->commentList = $commentList;
        }

        $this->view->commentId = $commentId;
        $this->view->menuSelected = 101;
        echo $this->view->render('comment.phtml');
    }

    public function linkAction ()
    {
        $filter = new Zend_Filter_Int();
        $pageNum = $filter->filter($this->_getParam('page', 1));
        $linkId = $filter->filter($this->_getParam('id'));

        if($linkId > 0){
            $linkInfo = $this->_linkModel->getLinkById($linkId);
            $this->view->linkInfo = $linkInfo;
        }else{
            $resultSet = $this->_linkModel->getAllLink();
            if(null === $resultSet){
                $linkList = array();
            } else {
                $perPageNum = 10;
                $linkList = Zend_Paginator::factory($resultSet);
                $linkList->setCurrentPageNumber($pageNum)->setItemCountPerPage($perPageNum);
            }
            $this->view->linkList = $linkList;
        }
        $this->view->linkType = $this->_linkModel->getAllLinkType();

        $this->view->linkId = $linkId;
        $this->view->menuSelected = 201;
        echo $this->view->render('link.phtml');
    }

    public function linktypeAction ()
    {
        $filter = new Zend_Filter_Int();
        $pageNum = $filter->filter($this->_getParam('page', 1));
        $linkTypeId = $filter->filter($this->_getParam('id'));

        if($linkTypeId > 0){
            $linkTypeInfo = $this->_linkModel->getLinkTypeById($linkTypeId);
            $this->view->linkTypeInfo = $linkTypeInfo;
        }else{
            $resultSet = $this->_linkModel->getAllLinkType();
            if(null === $resultSet){
                $linkTypeList = array();
            } else {
                $perPageNum = 10;
                $linkTypeList = Zend_Paginator::factory($resultSet);
                $linkTypeList->setCurrentPageNumber($pageNum)->setItemCountPerPage($perPageNum);
            }
            $this->view->linkTypeList = $linkTypeList;
        }

        $this->view->linkTypeId = $linkTypeId;
        $this->view->menuSelected = 202;
        echo $this->view->render('linktype.phtml');
    }

    public function pageAction ()
    {
        $filter = new Zend_Filter_Int();
        $pageNum = $filter->filter($this->_getParam('page', 1));
        $pageId = $filter->filter($this->_getParam('id'),0);

        if($pageId > 0){
            $pageInfo = $this->_pageModel->getPageById($pageId);
            $this->view->pageInfo = $pageInfo;
        }else{
            $resultSet = $this->_pageModel->getAllPage();
            if(null === $resultSet){
                $pageList = array();
            } else {
                foreach($resultSet as $page){
                    $page->publishTime = date('Y-m-d H:i:s', $page->publishTime);
                }
                $perPageNum = 10;
                $pageList = Zend_Paginator::factory($resultSet);
                $pageList->setCurrentPageNumber($pageNum)->setItemCountPerPage($perPageNum);
            }
            $this->view->pageList = $pageList;
        }

        $this->view->pageId = $pageId;
        $this->view->menuSelected = 302;
        echo $this->view->render('pagelist.phtml');
    }

    public function userAction ()
    {
        $filter = new Zend_Filter_Int();
        $pageNum = $filter->filter($this->_getParam('page', 1));

        $resultSet = $this->_userModel->getAllUser();

        foreach($resultSet as $user){
            $user->registerTime = date('Y-m-d H:i:s', $user->registerTime);
            $user->lastLoginTime = date('Y-m-d H:i:s', $user->lastLoginTime);
        }

        $perPageNum = 10;

        $userList = Zend_Paginator::factory($resultSet);
        $userList->setCurrentPageNumber($pageNum)->setItemCountPerPage($perPageNum);
        $this->view->userList = $userList;

        $this->view->menuSelected = 402;
        echo $this->view->render('userlist.phtml');

    }
}